// pages/shopInfo/shopInfo.js
const md5 = require('../../utils/md5.js')
const common = require('../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        shopName: '',
        userName: '',
        userLabel: '',
        sexArray: ['女', '男'],
        sexIndex: 0,
        birth: '1980',
        userPhone: '',
        region: [],
        shopAddress: '',
        imgAdd: 'https://www.meiboshi.vip/static/images/wx_sm/userCenter/shop-img-add.png',
        shopLogo: null,
        shopImg: null,
        concatImg: null,
        shopIntro: '',
        experienceDetail: '',
        shopExperience: [],
        isLoading: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.getShopInfo()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        this.getExperience()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {
        wx.switchTab({
            url: '/pages/user/userCenter/userCenter'
        })
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    getExperience: function() {
        const that = this
        let data = {
            store_id: wx.getStorageSync('storeId')
        }
        common.post('Store/get_store_experience', data).then(function(res) {
            if (res.code == 204) {
                return
            }
            that.setData({
                shopExperience: res
            })
        }).catch(function(e) {})
    },

    getShopInfo: function() {
        wx.showLoading({
            title: '加载中'
        })
        const that = this
        let data = {
            store_id: wx.getStorageSync('storeId')
        }
        common.post('Store/get_store', data).then(function(res) {
            wx.hideLoading()
            if (res.code == 204) {
                return
            } else {
                that.setData({
                    shopName: res.store_name,
                    userName: res.art_name,
                    userLabel: res.code,
                    sexArray: ['女', '男'],
                    sexIndex: res.sex ? res.sex : 0,
                    birth: res.age ? res.age : '1980',
                    userPhone: res.phone,
                    shopAddress: res.address,
                    shopLogo: res.store_logo ? res.store_logo : null,
                    shopImg: res.backgroundimg ? res.backgroundimg : null,
                    shopIntro: res.brief,
                    experienceDetail: res.experience
                })
            }
        }).catch(function(e) {})
    },

    sexChange: function(e) {
        this.setData({
            sexIndex: e.detail.value
        })
    },

    birthChange: function(e) {
        this.setData({
            birth: e.detail.value
        })
    },

    regionChange: function(e) {
        this.setData({
            region: e.detail.value
        })
    },

    shopNameInput: function(e) {
        this.setData({
            shopName: e.detail.value
        })
    },

    userNameInput: function(e) {
        this.setData({
            userName: e.detail.value
        })
    },

    userLabelInput: function(e) {
        this.setData({
            userLabel: e.detail.value
        })
    },

    phoneInput: function(e) {
        this.setData({
            userPhone: e.detail.value
        })
    },

    addressInput: function(e) {
        this.setData({
            shopAddress: e.detail.value
        })
    },

    shopIntroInput: function(e) {
        this.setData({
            shopIntro: e.detail.value
        })
    },

    experienceInput: function(e) {
        this.setData({
            experienceDetail: e.detail.value
        })
    },

    addExperience: function() {
        wx.navigateTo({
            url: '/pages/shopInfo/experience/experience?type=add'
        })
    },

    objKeySort: function(obj) {
        let newkey = Object.keys(obj).sort()
        let newObj = {}
        for (let i = 0; i < newkey.length; i++) {
            newObj[newkey[i]] = obj[newkey[i]]
        }
        return newObj
    },

    shopLogoChange: function() {
        const that = this
        let data = {}
        let key = wx.getStorageSync('token')
        let sign = 'cds'
        let timestamp = Date.parse(new Date())
        data.key = key
        data.timestamp = timestamp
        data = this.objKeySort(data)
        for (let i in data) {
            sign += (i + data[i])
        }
        sign += 'cds'
        sign = md5(sign)
        data.sign = sign
        wx.chooseImage({
            count: 1, // 默认9
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function(res) {
                wx.showLoading({
                    title: '正在上传图片',
                })
                let tempFilePaths = res.tempFilePaths
                wx.uploadFile({
                    url: 'https://www.meiboshi.vip/api/Upimg/up_store_background', //仅为示例，非真实的接口地址
                    filePath: tempFilePaths[0],
                    name: 'file',
                    formData: {
                        key: data.key,
                        timestamp: data.timestamp,
                        sign: data.sign
                    },
                    success(res) {
                        if (JSON.parse(res.data).code == 204) {
                            wx.hideLoading()
                            wx.showModal({
                                title: '提示',
                                content: JSON.parse(res.data).error,
                                showCancel: false
                            })
                            return
                        }
                        wx.hideLoading()
                        wx.showToast({
                            title: '上传图片成功',
                            icon: 'none',
                            duration: 1000
                        })
                        that.setData({
                            shopLogo: JSON.parse(res.data).datas
                        })
                    }
                })
            }
        })
    },

    shopImgChange: function() {
        const that = this
        let data = {}
        let key = wx.getStorageSync('token')
        let sign = 'cds'
        let timestamp = Date.parse(new Date())
        data.key = key
        data.timestamp = timestamp
        data = this.objKeySort(data)
        for (let i in data) {
            sign += (i + data[i])
        }
        sign += 'cds'
        sign = md5(sign)
        data.sign = sign
        wx.chooseImage({
            count: 1, // 默认9
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function(res) {
                wx.showLoading({
                    title: '正在上传图片',
                })
                let tempFilePaths = res.tempFilePaths
                wx.uploadFile({
                    url: 'https://www.meiboshi.vip/api/Upimg/up_store_background', //仅为示例，非真实的接口地址
                    filePath: tempFilePaths[0],
                    name: 'file',
                    formData: {
                        key: data.key,
                        timestamp: data.timestamp,
                        sign: data.sign
                    },
                    success(res) {
                        wx.hideLoading()
                        wx.showToast({
                            title: '上传图片成功',
                            icon: 'none',
                            duration: 1000
                        })
                        that.setData({
                            shopImg: JSON.parse(res.data).datas
                        })
                    }
                })
            }
        })
    },

    concatImgChange: function() {
        const that = this
        let data = {}
        let key = wx.getStorageSync('token')
        let sign = 'cds'
        let timestamp = Date.parse(new Date())
        data.key = key
        data.timestamp = timestamp
        data = this.objKeySort(data)
        for (let i in data) {
            sign += (i + data[i])
        }
        sign += 'cds'
        sign = md5(sign)
        data.sign = sign
        wx.chooseImage({
            count: 1, // 默认9
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function(res) {
                wx.showLoading({
                    title: '正在上传图片',
                })
                let tempFilePaths = res.tempFilePaths
                wx.uploadFile({
                    url: 'https://www.meiboshi.vip/api/Upimg/up_store_background', //仅为示例，非真实的接口地址
                    filePath: tempFilePaths[0],
                    name: 'file',
                    formData: {
                        key: data.key,
                        timestamp: data.timestamp,
                        sign: data.sign
                    },
                    success(res) {
                        wx.hideLoading()
                        wx.showToast({
                            title: '上传图片成功',
                            icon: 'none',
                            duration: 1000
                        })
                        that.setData({
                            concatImg: JSON.parse(res.data).datas
                        })
                    }
                })
            }
        })
    },

    submitInfo: function() {
        if (this.data.shopName == '') {
            wx.showToast({
                title: '请填写店铺名称',
                icon: 'none',
                duration: 2000
            })
            return
        }
        if (this.data.userName == '') {
            wx.showToast({
                title: '请填写个人艺名',
                icon: 'none',
                duration: 2000
            })
            return
        }
        if (!(/^1\d{10}$/.test(this.data.userPhone))) {
            wx.showToast({
                title: '手机号输入有误',
                icon: 'none',
                duration: 2000
            })
            return
        }
        // if (this.data.region == []) {
        //     wx.showToast({
        //         title: '请选择所属城市',
        //         icon: 'none',
        //         duration: 2000
        //     })
        //     return
        // }
        if (this.data.shopAddress == '') {
            wx.showToast({
                title: '请输入店铺地址',
                icon: 'none',
                duration: 2000
            })
            return
        }
        if (this.data.shopLogo == 'https://www.meiboshi.vip/static/images/wx_sm/userCenter/shop-img-add.png') {
            wx.showToast({
                title: '请上传店铺logo',
                icon: 'none',
                duration: 2000
            })
            return
        }
        if (this.data.shopImg == 'https://www.meiboshi.vip/static/images/wx_sm/userCenter/shop-img-add.png') {
            wx.showToast({
                title: '请上传背景主图',
                icon: 'none',
                duration: 2000
            })
            return
        }
        if (this.data.experienceDetail == '') {
            wx.showToast({
                title: '请输入纹绣经验',
                icon: 'none',
                duration: 2000
            })
            return
        }
        if (this.data.shopIntro == '') {
            wx.showToast({
                title: '请输入店铺简介',
                icon: 'none',
                duration: 2000
            })
            return
        }
        if (this.data.isLoading) {
            wx.showToast({
                title: '请勿重复提交',
                icon: 'none',
                duration: 2000
            })
            return
        }
        const that = this
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '信息提交中'
        })
        let data = {
            store_name: this.data.shopName,
            backgroundimg: this.data.shopImg,
            brief: this.data.shopIntro,
            experience: this.data.experienceDetail.replace(/\n|\r\n/g, "<br>"),
            phone: this.data.userPhone,
            address: this.data.shopAddress,
            store_logo: this.data.shopLogo,
            art_name: this.data.userName,
            age: this.data.birth,
            sex: this.data.sexIndex,
            linkimg: this.data.concatImg,
            code: this.data.userLabel
        }
        common.post('Store/up_store', data).then(function(res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                wx.showToast({
                    title: res.error,
                    icon: 'none',
                    duration: 2000
                })
            } else {
                wx.showModal({
                    title: '提示',
                    content: '信息提交成功',
                    showCancel: false,
                    success(res) {
                        if (res.confirm) {
                            wx.switchTab({
                                url: '/pages/user/userCenter/userCenter'
                            })
                        }
                    }
                })
            }
        }).catch(function(e) {})
    },

    goEdit: function(e) {
        wx.setStorageSync('experienceInfo', e.currentTarget.dataset.info)
        wx.navigateTo({
            url: '/pages/shopInfo/experience/experience?type=edit'
        })
    }
})